package com.ikangtai.shecare.stickycalendar.fragment.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.ikangtai.shecare.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 按钮数组，两个字符串数组names、codes，按钮上面显示names的内容，点击按钮 可以选中某个按钮并且根据codes的内容来判断选中的哪个按�?
 */
public class GroupButtonView extends LinearLayout {

    RadioGroup mRg;

    private List<RadioButton> viewList;
    private Context mContext;
    private OnGroupBtnClickListener listener;

    private String groupBtnName = "A#B";
    private String groupBtnCode = "1#2";

    public String getN() {
        return n;
    }

    public void setN(String n) {
        this.n = n;
    }

    private String n = null;

    public GroupButtonView(Context context) {
        this(context, null);
    }

    public GroupButtonView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GroupButtonView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.GroupButtonView);
        groupBtnName = ta.getString(R.styleable.GroupButtonView_groupBtnNameText);
        groupBtnCode = ta.getString(R.styleable.GroupButtonView_groupBtnCodeText);

        ta.recycle();
        viewList = new ArrayList<>();
        initContentView();
        initView();

        initData(n);
    }

    private void initContentView() {
        View.inflate(mContext, R.layout.view_item_group_buttons, this);
        mRg = (RadioGroup) findViewById(R.id.rg_item_group_btn);

    }

    private void initView() {
        String[] btnNameArr = groupBtnName.split("#");
        String[] btnCodeArr = groupBtnCode.split("#");

        Log.d("gyw", btnNameArr.length + "   length  :" + btnCodeArr.length);

        if ((btnNameArr.length < 2) || (btnCodeArr.length < 2)) {
            throw new RuntimeException("names和codes的格式不正确...");
        }

        for (int i = 0; i < btnCodeArr.length; i++) {

            //使用布局文件写方便
            RadioButton rb = (RadioButton) View.inflate(mContext, R.layout.view_item_group_radio_button, null);
//
            RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(10, 0, 10, 0);
            rb.setLayoutParams(layoutParams);

            rb.setBackgroundResource(R.drawable.mid_button_selector);

            rb.setId(i);
            rb.setTag(btnCodeArr[i]);
            rb.setText(btnNameArr[i]);

            viewList.add(rb);
            mRg.addView(rb);
        }
    }

    private void initData(String name) {
        mRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup rg, int i) {
                RadioButton rb = (RadioButton) findViewById(i);
                if (rb.isChecked()) {
                    if (listener != null) {
                        listener.groupBtnClick(rb.getTag().toString());
                    }
                }
            }
        });

    }


    public List<RadioButton> getViewList() {
        return viewList;
    }

    public void setViewList(List<RadioButton> viewList) {
        this.viewList = viewList;
    }

    public interface OnGroupBtnClickListener {
        public void groupBtnClick(String code);
    }

    public void setOnGroupBtnClickListener(OnGroupBtnClickListener listener) {
        this.listener = listener;
    }
}
